package com.socodethis.spaceex.mainmenuscreen;

import com.badlogic.gdx.Audio;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.socodethis.spaceex.Assets;
import com.socodethis.spaceex.SpaceExConstants;
import com.socodethis.spaceex.customGame;
import com.socodethis.spaceex.mainmenuscreen.actors.MenuButton;
import com.socodethis.spaceex.utils.ScreenSize;

public class MainMenuScreen implements Screen {

	private customGame game;
	private MenuButton logo;
	private MenuButton planetLarge;
	private MenuButton planetSmall;
	private MenuButton btnPlay;
	private MenuButton btnExit;
	private Stage stage;
	private SpriteBatch spriteBatch;
	private Music themeMusic;
	
	public MainMenuScreen(customGame g){
		game = g;
		spriteBatch=new SpriteBatch();
		themeMusic = Gdx.audio.newMusic(Gdx.files.internal("sounds/theme_evening_star.mp3"));
		themeMusic.setVolume(0.5f);
		themeMusic.setLooping(true);   
		themeMusic.play();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		stage.act(delta);
		spriteBatch.begin();
		for(Actor mb : stage.getActors()){
			MenuButton b = (MenuButton)mb;
			if (b.planetOrbitFront!=null)
				if (b.planetOrbitFront)
					b.draw(spriteBatch, 0);
		}
		for(Actor mb : stage.getActors()){
			MenuButton b = (MenuButton)mb;
			b.update(game);
			if (b.planetOrbitFront==null)
				b.draw(spriteBatch, 0);
			else if (!b.planetOrbitFront)
				b.draw(spriteBatch, 0);
		}
		spriteBatch.end();
		
	}

	@Override
	public void resize(int width, int height) {
		
	}

	@Override
	public void show() {
		stage = new Stage(SpaceExConstants.SCREEN_WIDTH,SpaceExConstants.SCREEN_HEIGHT,false);
		
		Texture texLargePlanet = Assets.manager.get("mediumPlanet9.png", Texture.class);
		planetLarge = new MenuButton((Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(texLargePlanet.getWidth())/2),
				Gdx.graphics.getHeight()-(int)ScreenSize.getScaledHeight(texLargePlanet.getHeight())-30,
				texLargePlanet);
		planetLarge.name="planetLarge";
		stage.addActor(planetLarge);
		
		Texture texSmallPlanet = Assets.manager.get("smallPlanet9.png", Texture.class);
		planetSmall = new MenuButton((Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(texSmallPlanet.getWidth())/2),
				(int)(planetLarge.y+((int)ScreenSize.getScaledHeight(texLargePlanet.getHeight())/2)),
				texSmallPlanet);
		planetSmall.name="planetSmall";
		planetSmall.planetOrbitFront=false;
		stage.addActor(planetSmall);
		
		Texture texLogo = Assets.manager.get("astrocat_logo.png", Texture.class);
		logo = new MenuButton((Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(texLogo.getWidth())/2),
				(Gdx.graphics.getHeight()/2)-((int)ScreenSize.getScaledHeight(texLogo.getHeight())/2),
				texLogo);
		stage.addActor(logo);
		
		Texture texPlay = Assets.manager.get("btnPlay.png", Texture.class);
		btnPlay = new MenuButton((Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(texPlay.getWidth())/2),
				(Gdx.graphics.getHeight()/2)-((int)ScreenSize.getScaledHeight(texPlay.getHeight())*2),
				texPlay);
		stage.addActor(btnPlay);
		
		Texture texExit = Assets.manager.get("btnExit.png", Texture.class);
		btnExit = new MenuButton((Gdx.graphics.getWidth()/2)-((int)ScreenSize.getScaledWidth(texExit.getWidth())/2),
				(Gdx.graphics.getHeight()/2)-((int)ScreenSize.getScaledHeight(texExit.getHeight())*3)-10,
				texExit);
		stage.addActor(btnExit);
	}

	@Override
	public void hide() {
		themeMusic.stop();
	}

	@Override
	public void pause() {
		themeMusic.stop();
	}

	@Override
	public void resume() {
		themeMusic.play();
	}

	@Override
	public void dispose() {
		themeMusic.dispose();
	}

}
